class oscInMod extends Mod
{
  oscInMod(Mod tempParent, float tempBx, float tempBy, int tempIndex) {
    super(tempParent, tempBx, tempBy, tempIndex);
    img="osc";
    loadImg(img);
    name="in";
    minWx=48;
    minWy=48;
    oPs[op]=new outPort(this, 1, "");
    iPs[ip]=new inPort(this, 2, "index");
    iPs[0].setShowUnused(true);
    updatePorts();
    isInput=true;
    val=0;
    oscHandler[val].addListener(this);
  }

  void go() {
    if (oPs[0].ws>0) {
      sendString(message, 0);
      doStack();
    }
  }
  void update(int tempI) {
    int portIO=floor(iPs[0].floatV);
    if (!iPs[0].isUsed || portIO>7 || portIO<0) {
      portIO=0;
    }
    if (portIO!=val) {
      oscHandler[val].removeListener(this);
      val=portIO;
      oscHandler[val].addListener(this);
    }
  }
}

class oscOutMod extends Mod
{
  oscOutMod(Mod tempParent, float tempBx, float tempBy, int tempIndex) {
    super(tempParent, tempBx, tempBy, tempIndex);
    img="osc";
    loadImg(img);
    name="out";
    minWx=48;
    minWy=48;
    iPs[ip]=new inPort(this, 1, "");
    iPs[ip]=new inPort(this, 2, "index");
    iPs[1].setShowUnused(true);
    updatePorts();
    val=0;
  }

  void go() {
    oscHandler[val].sendOSC(iPs[0].stringV);
 }
  void update(int tempI) {
    if (tempI==130) {
      int portIO=floor(iPs[0].floatV);
      if (!iPs[0].isUsed || portIO>7 || portIO<0) {
        portIO=0;
      }
      val=portIO;
    }
  }
}


class oscRemAddrMod extends Mod
{
  oscRemAddrMod(Mod tempParent, float tempBx, float tempBy, int tempIndex) {
    super(tempParent, tempBx, tempBy, tempIndex);
    img="rmaddr";
    loadImg(img);
    name="address";
    minWx=48;
    minWy=48;
    iPs[ip]=new inPort(this, 1, "");
    iPs[ip]=new inPort(this, 1, "prefix");
    oPs[op]=new outPort(this, 1, "");
    oPs[op]=new outPort(this, 1, "prefix");
    updatePorts();
    iPs[1].setShowUnused(true);
  }

  void go() {
    String[] tokens=splitTokens(iPs[0].stringV, "/");
    if (tokens[0]!=null) {
      String ss="";
      if (tokens.length>0) {
        for (int i=1;i<tokens.length;i++) {
          ss=ss+"/"+tokens[i];
        }
      }
      if (!iPs[1].isUsed) {
        sendString(ss, 0);
      }
      else if (iPs[1].stringV!=null) {
        if (iPs[1].stringV.equals("/"+tokens[0])) {
          sendString(ss, 0);
        }
      }
      sendString("/"+tokens[0], 1);
    }
  }
}
class oscAddAddrMod extends Mod
{
  oscAddAddrMod(Mod tempParent, float tempBx, float tempBy, int tempIndex) {
    super(tempParent, tempBx, tempBy, tempIndex);
    img="addaddr";
    loadImg(img);
    name="address";
    minWx=48;
    minWy=48;
    iPs[ip]=new inPort(this, 1, "");
    iPs[ip]=new inPort(this, 1, "prefix");
    oPs[op]=new outPort(this, 1, "");
    updatePorts();
  }

  void go() {
    String message=iPs[0].stringV;
    sendString(iPs[1].stringV+message, 0);
  }
}
class oscGetMsgMod extends Mod
{
  oscGetMsgMod(Mod tempParent, float tempBx, float tempBy, int tempIndex) {
    super(tempParent, tempBx, tempBy, tempIndex);
    img="oscmsg";
    loadImg(img);
    name="value";
    minWx=48;
    minWy=48;
    iPs[ip]=new inPort(this, 1, "");
    iPs[ip]=new inPort(this, 1, "message");
    oPs[op]=new outPort(this, 2, str(op+1));
    updatePorts();
    iPs[1].setShowUnused(true);
    oPs[0].setShowUnused(true);
    oPs[0].setAddPort(true);
    oPs[0].setShowIndex(true);
    oPs[0].indexOff=1;
  }

  void go() {
    String[] tokens=splitTokens(iPs[0].stringV, " /");
    if (tokens[0]!=null) {
      Boolean out=false;
      if (!iPs[1].isUsed) {
        out=true;
      }
      else if (iPs[1].stringV.equals("/"+tokens[0])) {
        out=true;
      }
      if (out) {
        for (int i=0;i<min(tokens.length-1,op);i++) {
          sendFloat(float(tokens[i+1]), i);
        }
      }
    }
  }
}

class oscSetMsgMod extends Mod
{
  oscSetMsgMod(Mod tempParent, float tempBx, float tempBy, int tempIndex) {
    super(tempParent, tempBx, tempBy, tempIndex);
    img="oscaddmsg";
    loadImg(img);
    name="value";
    minWx=48;
    minWy=48;
    iPs[ip]=new inPort(this, 1, "message");
    iPs[ip]=new inPort(this, 2, str(ip));
    oPs[op]=new outPort(this, 1, "");
    iPs[0].trigger=true;

    updatePorts();
    iPs[1].setShowUnused(true);
    iPs[1].setAddPort(true);
    iPs[1].setShowIndex(true);
  }

  void go() {
    String out="";
    out=out+iPs[0].stringV;
    for (int i=1;i<ip;i++) {
      if (iPs[i].isUsed) {
        out=out+" "+str(int(iPs[i].floatV));
      }
    }
    sendString(out, 0);
  }
}

class oscInfoMod extends Mod
{
  oscInfoMod(Mod tempParent, float tempBx, float tempBy, int tempIndex) {
    super(tempParent, tempBx, tempBy, tempIndex);
    img="osc";
    loadImg(img);
    name="info";
    minWx=48;
    minWy=48;

    iPs[ip]=new inPort(this, 2, "index");
    oPs[op]=new outPort(this, 1, "name");
    oPs[op]=new outPort(this, 1, "ip");
    oPs[op]=new outPort(this, 2, "send");
    oPs[op]=new outPort(this, 2, "recieve");

    updatePorts();
  }

  void go() {
    update(0);
  }
  void update(int tempI) {
    println(tempI);
    int portIO=floor(iPs[0].floatV);
    if (!iPs[0].isUsed || portIO>7 || portIO<0) {
      sendString("", 0);
      sendString("", 1);
      sendFloat(0.0, 2);
      sendFloat(0.0, 3);
    }
    else {
      sendString(oscHandler[portIO].name, 0);
      sendString(oscHandler[portIO].remoteIP, 1);
      sendFloat((float)oscHandler[portIO].sendPort, 2);
      sendFloat((float)oscHandler[portIO].recievePort, 3);
    }
  }
}

